package com.nhim.cms.web;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.IMarkupCacheKeyProvider;
import org.apache.wicket.markup.IMarkupResourceStreamProvider;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.resource.IResourceStream;
import org.apache.wicket.util.resource.StringResourceStream;

import com.nhim.cms.CustomWidgetProperty.EDataType;
import com.nhim.cms.cache.management.CacheDataLoader;
import com.nhim.cms.domain.CustomWidgetInstance;
import com.nhim.cms.domain.CustomWidgetPropertyValue;
import com.nhim.cms.util.CustomWidgetInstanceManager;
import com.nhim.core.ReflectionHelper;

public class BasePage extends WebPage implements IMarkupResourceStreamProvider,
		IMarkupCacheKeyProvider {
	private static final Log log = LogFactory.getLog(BasePage.class);
	private static final long serialVersionUID = 1L;
	
	 @SpringBean
	   private CustomWidgetInstanceManager customWidgetInstanceManager;


	private StringBuilder markupString = new StringBuilder();
	private static final String EMPTY_MARKUP = "<html><body></body></html>";

	protected void buildPage() {
		//add(new Label("version", getApplication().getFrameworkSettings().getVersion()));
		//add(loadNhimWidget(customWidgetInstanceManager.findById(1)));
		markupString.append(CacheDataLoader.getInstance().getTemplate());

	}

	private BasePanel loadNhimWidget(CustomWidgetInstance widgetInstance) {
		BasePanel basePanel = null;
		try {
			basePanel = (BasePanel) ReflectionHelper.createInstanceFrom(widgetInstance.getCustomWidget().getClassName(), widgetInstance.getName());
			for (CustomWidgetPropertyValue cwValue : widgetInstance.getCustomWidgetPropertyValues()) 
			{
				switch (EDataType.valueOf(cwValue.getDataType())) 
				{
					case String: 
					{
						Method method = basePanel.getClass().getMethod(cwValue.getName(), String.class);
						method.invoke(basePanel, cwValue.getValue());
						break;
					}
					case Boolean:
					{
						Method method = basePanel.getClass().getMethod(cwValue.getName(), Boolean.class);
						method.invoke(basePanel, Boolean.parseBoolean(cwValue.getValue()));
						break;
					}
					case Integer:
					{
						Method method = basePanel.getClass().getMethod(cwValue.getName(), Integer.class);
						method.invoke(basePanel, Integer.parseInt(cwValue.getValue()));
						break;
					}
					case Float:
					{
						Method method = basePanel.getClass().getMethod(cwValue.getName(), Float.class);
						method.invoke(basePanel,  Float.parseFloat(cwValue.getValue()));
						break;
					}
					case DateTime:
					{
						Method method = basePanel.getClass().getMethod(cwValue.getName(), Date.class);
				
						try {
							method.invoke(basePanel,DateFormat.getDateTimeInstance().parse(cwValue.getValue()));
						} catch (ParseException e) {
							
							log.error(e);
						}
						break;
					}
				}
			}
			basePanel.init();

		} catch (SecurityException e) {
			log.error(e);

		} catch (IllegalArgumentException e) {

			log.error(e);

		} catch (ClassNotFoundException e) {

			log.error(e);

		} catch (NoSuchMethodException e) {

			log.error(e);

		} catch (InstantiationException e) {

			log.error(e);

		} catch (IllegalAccessException e) {

			log.error(e);

		} catch (InvocationTargetException e) {

			log.error(e);
		}
		return basePanel;

	}

	private String getMarkupString() {
		return markupString.length() == 0 ? EMPTY_MARKUP : markupString
				.toString();

	}

	public String getCacheKey(MarkupContainer arg0, Class<?> arg1) {
		return null;
	}

	public IResourceStream getMarkupResourceStream(MarkupContainer arg0,
			Class<?> arg1) {

		return new StringResourceStream(getMarkupString());

	}

}
